<?php

namespace Openfennec\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * @ORM\Table(name="openfennec_users")
 * @ORM\Entity
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="username", type="string", length=25, unique=true)
     */
    private $username;
    
    /**
     * @ORM\Column(name="nick", type="string", length=30)
     */
    private $nick;

    /**
     * @ORM\Column(name="email", type="string", length=50, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(name="salt", type="string", length=32)
     */
    private $salt;

    /**
     * @ORM\Column(name="password", type="string", length=255)
     */
    private $password;

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;
    
    /**
     * @ORM\Column(name="roles", type="json_array")
     */
    private $roles;
    
    /**
     * @ORM\Column(name="is_first_login", type="boolean")
     */
    private $isFirstLogin;
    
    /**
     * @ORM\Column(name="date_created", type="date")
     */
    private $dateCreated;
    
    public function __construct() {
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
        $this->isFirstLogin = true;
        $this->dateCreated = new \DateTime("now");
        $this->roles = array("ROLE_USER");
    }

    public function getId()
    {
        return $this->id;
    }
    
    public function setUsername($username)
    {
        $this->username = $username;
        return $this;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setSalt($salt)
    {
        $this->salt = $salt;
        return $this;
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
        return $this;
    }

    public function isEnabled()
    {
        return $this->isActive;
    }
    
    public function getIsFirstLogin() {
        return $this->isFirstLogin;
    }

    public function getDateCreated() {
        return $this->dateCreated;
    }

    public function setIsFirstLogin($isFirstLogin) {
        $this->isFirstLogin = $isFirstLogin;
        return $this;
    }

    public function setDateCreated($dateCreated) {
        $this->dateCreated = $dateCreated;
        return $this;
    }
    
    public function getNick() {
        return $this->nick;
    }

    public function setNick($nick) {
        $this->nick = $nick;
    }
    
    public function serialize (){
        return serialize(array(
            $this->id,
            $this->username,
            $this->email,
            $this->salt,
            $this->password,
        ));
    }
    
    public function unserialize ($serialized){
        list (
            $this->id,
            $this->username,
            $this->email,
            $this->salt,
            $this->password,
        ) = unserialize($serialized);
}

    public function eraseCredentials() {}

    public function getRoles() {
        return $this->roles;
    }

    public function isAccountNonExpired() {
        return true;
    }

    public function isAccountNonLocked() {
        return true;
    }

    public function isCredentialsNonExpired() {
        return true;
    }
}
